var dragNclick = (function(elem, menu) {
        var bTime = 0,
            eTime = 0,
            oPos = [],
            cbTime = 0,
            ceTime = 0,
            counter = 0,
            t = null,
            wWidth = getViewPortSize().width,
            wHeight = getViewPortSize().height,
            eleWidth = getStyle(elem, 'width'),
            eleHeight = getStyle(elem, 'height'),
            mWidth = getStyle(menu, 'width'),
            mHeight = getStyle(menu, 'height');

        elemDrag(elem);

        function elemDrag(elem) {
            var x,
                y;
            addEvent(elem, 'mousedown', function(e) {
                    var e = e || window.event;
                    btnCode = e.button;
                    // 判断右击
                    if (btnCode == 2) {
                        var mLeft = pagePos(e).x,
                            mTop = pagePos(e).y;
                        if (mLeft <= 0) {
                            mLeft = 0
                                // 当鼠标位置大于视口宽度减去元素尺寸，说明位置已经到边界了
                        } else if (mLeft >= wWidth - mWidth) {
                            mLeft = pagePos(e).x - mWidth;
                        }
                        if (mTop <= 0) {
                            mTop = 0
                        } else if (mTop >= wHeight - mHeight) {
                            mTop = pagePos(e).y - mHeight;
                        }
                        menu.style.left = mLeft + 'px';
                        menu.style.top = mTop + 'px';
                        menu.style.display = 'block';
                    } else if (btnCode == 0) {
                        bTime = new Date().getTime();
                        oPos = [getStyle(elem, 'left'), getStyle(elem, 'top')]
                        menu.style.display = 'none';
                        x = pagePos(e).x - getStyle(elem, 'left');
                        y = pagePos(e).y - getStyle(elem, 'top');
                        addEvent(document, 'mousemove', mouseMove);
                        addEvent(document, 'mouseup', mouseUp);
                    }
                })
                // 阻止默认事件
            addEvent(document, 'contextmenu', function(e) {
                var e = e || window.event;
                preventDefaultEvent(e)
            });
            //点击其他位置消失
            addEvent(document, 'click', function(e) {
                    menu.style.display = 'none'
                })
                //阻止冒泡
            addEvent(menu, 'click', function(e) {
                var e = e || window.event;
                vancelBubble(e)
            })

            function mouseMove(e) {
                var e = e || window.event,
                    eleLeft = pagePos(e).x - x,
                    eleTop = pagePos(e).y - y;
                // 限制移动位置
                if (eleLeft <= 0) {
                    eleLeft = 0
                } else if (eleLeft >= wWidth - eleWidth) {
                    eleLeft = wWidth - eleWidth - 1;
                }
                if (eleTop <= 0) {
                    eleTop = 0;
                } else if (eleTop >= wHeight - eleHeight) {
                    eleTop = wHeight - eleHeight - 1;
                }
                elem.style.left = eleLeft + 'px';
                elem.style.top = eleTop + 'px';

            }

            function mouseUp() {
                var e = e || window.event;
                eTime = new Date().getTime();
                if (eTime - bTime < 300) {
                    console.log('mouseUp');
                    console.log(oPos[0]);

                    // 元素归位
                    elem.style.left = oPos[0] + 'px';
                    elem.style.top = oPos[1] + 'px';
                    counter++;
                    // 双击
                    if (counter == 1) {
                        cbTime = new Date().getTime();
                    }
                    if (counter == 2) {
                        ceTime = new Date().getTime();
                    }
                    console.log(ceTime);
                    if (cbTime && ceTime && (ceTime - cbTime < 300)) {
                        console.log('双击');
                        window.open('http://www.baidu.com');

                    }
                    t = setTimeout(() => {
                        cbTime = 0;
                        ceTime = 0;
                        counter = 0;
                        clearTimeout(t);
                    }, 500);
                }
                removeEvent(document, 'mousemove', mouseMove);
                removeEvent(document, 'mouseup', mouseUp);
            }
        }
    })
    // 鼠标左中右对应的值 0，1，2
    // 值存在e.button中